<?php

namespace App\Http\Controllers\Wechat\Messages;

use Carbon\Carbon;
use Laravel\Lumen\Routing\Controller as BaseController;
use EasyWeChat\Foundation\Application;
use Illuminate\Support\Facades\Cache;


/**
 * 图文消息
 * Class News
 * @package App\Http\Controllers\Wechat\Messages
 */
class News extends BaseController
{


    /**
     * 微信消息 图文 注册(绑定)
     * @param $message
     * @return array
     */
    public static function register($message)
    {
        $news[] = new \EasyWeChat\Message\News([
            'title' => '尚未绑定学号',
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => "回复：绑定+学号+密码",
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => "例如：绑定1305040301passwd",
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => "目前仅对大连工业大学本科生开放\n注：中间不要有空格",
        ]);
        return $news;
    }

    /**
     * 天气查询结果
     * @param $message
     * @param $data
     * @return array
     */
    public static function weather($message, $data)
    {
//        $cacheKey = 'weather_dalian';
//
//        if (Cache::get($cacheKey)) {
//            return unserialize(Cache::get($cacheKey));
//        }

        $news = [];

        $news[] = new \EasyWeChat\Message\News([
            'title' => '天气',
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => '实时 ' . $data['realtime']['weather']['temperature'] . '°C  ' . $data['realtime']['weather']['info'] . '  ' . $data['realtime']['wind']['direct'] . $data['realtime']['wind']['power'],
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => '明天 ' . $data['weather'][1]['info']['night'][2] . '~' . $data['weather'][1]['info']['day'][2] . '°C  ' . $data['weather'][1]['info']['dawn'][1] . '  ' . $data['weather'][1]['info']['dawn'][3] . $data['weather'][1]['info']['dawn'][4],
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => '后天 ' . $data['weather'][2]['info']['night'][2] . '~' . $data['weather'][2]['info']['day'][2] . '°C  ' . $data['weather'][2]['info']['dawn'][1] . '  ' . $data['weather'][2]['info']['dawn'][3] . $data['weather'][2]['info']['dawn'][4],
        ]);

//        Cache::put($cacheKey, serialize($news), Carbon::now()->addHour());
        return $news;
    }


    /**
     * 期末考试成绩
     * @param $openId
     * @param $scoreFinal
     * @return array|mixed|null
     */
    public static function scoreFinal($openId, $scoreFinal)
    {
        $cacheKey = 'score_final_' . $openId;

        // 优先从缓存获取
        if (Cache::get($cacheKey)) {
            $news = unserialize(Cache::get($cacheKey));
            return \App\Library\News::chunkSendByStaff($news, $openId);
        }


        // 缓存没有
        // 本学期考试成绩教务处还没有发布
        if (count($scoreFinal) == 1) {
            $news[] = new \EasyWeChat\Message\News([
                'title' => "本学期成绩尚未发布"
            ]);
            $news[] = new \EasyWeChat\Message\News([
                'title' => "点此 分享成绩单(本学期)",
                'url'   => url('scores', ['type' => time(), 'openid' => $openId])
            ]);
            $news[] = new \EasyWeChat\Message\News([
                'title' => "点此 分享成绩单(所有学期)",
                'url'   => url('scores', ['type' => 'all', 'openid' => $openId])
            ]);

            Cache::put($cacheKey, serialize($news), Carbon::now()->addHours(3));

            return $news;
        }

        // 教务处已发布
        $news[] = new \EasyWeChat\Message\News([
            'title' => '学年学期：' . $scoreFinal[1][1]
        ]);

        foreach ($scoreFinal as $key => $item) {
            if ($key > 0) {
                $type   = ($item[10] == '正常考试') ? null : ('(' . $item[10] . ')');
                $news[] = new \EasyWeChat\Message\News([
                    'title' => "课程名称：$item[3]\n考试成绩：$item[4] $type"
                ]);
                if ((count($news) % 8) === 0) {
                    $news[] = new \EasyWeChat\Message\News([
                        'title' => '学年学期：' . $scoreFinal[1][1]
                    ]);
                }
            }
        }

        $news[] = new \EasyWeChat\Message\News([
            'title' => "点此 分享成绩单(本学期)",
            'url'   => url('scores', ['type' => time(), 'openid' => $openId])
        ]);
        $news[] = new \EasyWeChat\Message\News([
            'title' => "点此 分享成绩单(所有学期)",
            'url'   => url('scores', ['type' => 'all', 'openid' => $openId])
        ]);

        Cache::put($cacheKey, serialize($news), Carbon::now()->addHours(3));

        return \App\Library\News::chunkSendByStaff($news, $openId);
    }


    /**
     * 等级考试成绩
     * @param $openId
     * @param $levelScores
     * @return null
     */
    public static function levelScores($openId, $levelScores)
    {
        $cacheKey = 'level_scores_' . $openId;

        // 优先从缓存获取
        if (Cache::get($cacheKey)) {
            $news = unserialize(Cache::get($cacheKey));
            return \App\Library\News::chunkSendByStaff($news, $openId);
        }

        $news[] = new \EasyWeChat\Message\News([
            'title' => '等级考试'
        ]);

        foreach ($levelScores as $key => $item) {
            if ($key > 0) {
                $score  = (is_null($item[4])) ? $item[7] : $item[4];
                $news[] = new \EasyWeChat\Message\News([
                    'title' => "$item[1]\n分数等级：$score\t\t考试时间：$item[8]"
                ]);
                if ((count($news) % 8) === 0) {
                    $news[] = new \EasyWeChat\Message\News([
                        'title' => '等级考试'
                    ]);
                }
            }
        }

        $news[] = new \EasyWeChat\Message\News([
            'title' => "以上是大学期间所有等级考试成绩"
        ]);

        Cache::put($cacheKey, serialize($news), Carbon::now()->addHours(3));

        return \App\Library\News::chunkSendByStaff($news, $openId);
    }

    /**
     * 单日课表
     *
     * $timetable   周课表       例如:第一周
     * $session    节次         例如: 第一周第1-2节
     * $sessionDay 节次周次     例如: 第一周周二第1-2节
     *
     * @param $openId
     * @param $timetable array 本周课表
     * @return null
     */
    public static function timetable($openId, $timetable)
    {
        if (is_null($timetable)) {
            return new \EasyWeChat\Message\News(['title' => '今明两天无课']);
        }
        $app = new Application(config('wechat'));

        $cacheKey = 'timetable_' . $openId;

        // 优先从缓存获取
        if (Cache::get($cacheKey)) {
            $news = unserialize(Cache::get($cacheKey));

            $app->staff->message($news['today'])->to($openId)->send();
            $app->staff->message($news['tomorrow'])->to($openId)->send();

            return null;
        }

        // 缓存没有
        $news['today'][] = new \EasyWeChat\Message\News([
            'title' => '今天课表'
        ]);

        $news['tomorrow'][] = new \EasyWeChat\Message\News([
            'title' => '明天课表'
        ]);

        foreach ($timetable as $key => $session) {
            foreach ($session as $day => $sessionDay) {
                if ($day == \App\Library\Timetable::today()) {
                    $news['today'][] = new \EasyWeChat\Message\News([
                        'title' => \App\Library\Timetable::TimetoHans($key) . "\t\t" . $sessionDay[0] . "\n" . $sessionDay[2] . "\t\t" . $sessionDay[3]
                    ]);
                }
                if ($day == \App\Library\Timetable::tomorrow()) {
                    $news['tomorrow'][] = new \EasyWeChat\Message\News([
                        'title' => \App\Library\Timetable::TimetoHans($key) . "\t\t" . $sessionDay[0] . "\n" . $sessionDay[2] . "\t\t" . $sessionDay[3]
                    ]);
                }
            }
        }

        $app->staff->message($news['today'])->to($openId)->send();
        $app->staff->message($news['tomorrow'])->to($openId)->send();

        Cache::put($cacheKey, serialize($news), Carbon::now()->addHours(3));

        return null;
    }


    /**
     * 点击菜单 快递追踪 时返回的信息
     * @return array
     */
    public static function expressTracking()
    {
        $news0 = new \EasyWeChat\Message\News([
            'title' => '试用快递实时追踪服务',
        ]);
        $news1 = new \EasyWeChat\Message\News([
            'title' => "回复：快递+单号+备注",
        ]);
        $news2 = new \EasyWeChat\Message\News([
            'title' => "例0：快递1151302195208\n例1：快递1151302195208阿里云发票\n例2：快递 1151302195208 阿里云发票",
        ]);
        $news3 = new \EasyWeChat\Message\News([
            'title' => "备注的第一个字若为数字，请在单号与备注之间添加空格",
        ]);

        return [$news0, $news1, $news2, $news3];
    }


    /**
     * 等级成绩查询
     * @return array
     */
    public static function scoreCet()
    {
        $news0 = new \EasyWeChat\Message\News([
            'title' => '大学英语四六级成绩查询',
        ]);
        $news1 = new \EasyWeChat\Message\News([
            'title' => "回复：cet+姓名+准考证号",
        ]);
        $news2 = new \EasyWeChat\Message\News([
            'title' => "例0：cet李白211070161201234\n例1：cet 李白 211070161201234",
        ]);

        return [$news0, $news1, $news2];
    }


    /**
     * 反馈建议
     * @return array
     */
    public static function feedback()
    {
        $news0 = new \EasyWeChat\Message\News([
            'title' => '反馈建议',
        ]);
        $news1 = new \EasyWeChat\Message\News([
            'title' => "回复：反馈+您想要说的话",
        ]);
        $news2 = new \EasyWeChat\Message\News([
            'title' => "例0：反馈建议添加图书查询功能\n例1：反馈 建议添加教务通知功能",
        ]);

        return [$news0, $news1, $news2];
    }


    /**
     * 考试安排信息
     *
     * @param $openId
     * @param $timetable
     * @return array
     */
    public static function examsinfo($openId, $examsinfo)
    {

        $cacheKey = 'examsinfo' . $openId;

        // 优先从缓存获取
        if ( Cache::get( $cacheKey ) ) {
            $news = unserialize( Cache::get( $cacheKey ) );
            return \App\Library\News::chunkSendByStaff( $news, $openId );
        }

        $news[] = new \EasyWeChat\Message\News([
            'title' => '考试安排'
        ]);

        foreach ($examsinfo as $key => $item) {
            if ($key > 0) {
                $news[] = new \EasyWeChat\Message\News([
                    'title' => "课程：$item[3]\n时间：$item[4]\t\t考场：$item[5]"
                ]);
                if ((count($news) % 8) === 0) {
                    $news[] = new \EasyWeChat\Message\News([
                        'title' => '等级考试'
                    ]);
                }
            }
        }

        if (count($examsinfo) == 1) {
            $news[] = new \EasyWeChat\Message\News([
                'title' => "教务处暂未发布你的考试安排信息"
            ]);
        } else {
            $news[] = new \EasyWeChat\Message\News([
                'title' => "以上是本学期考试安排"
            ]);
        }


        Cache::put( $cacheKey, serialize( $news ), Carbon::now()->addHours( 3 ) );

        return \App\Library\News::chunkSendByStaff($news, $openId);
    }

}
